using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutBillDetailDataAccess : FreshCmsAccessBase
    {
        #region code CMS
        public List<OutBillDetail> GetOutBillDetailList()
        {
            try
            {
                const string spName = "spo_OutBillDetail_GetList";

                var OutBillDetailList = DbHelper.ExecuteSPList<OutBillDetail>(spName);
                return OutBillDetailList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutBillDetail>();
            }
        }

        public List<OutBillDetail> GetOutBillDetailListByPaging(int pageIndex, int pageSize, ref int totalRecord)
        {
            try
            {
                const string spName = "spo_OutBillDetail_GetByPaging";
                var sqlParams = new[]
                                    {
										new SqlParameter("@PageIndex", pageIndex),
										new SqlParameter("@PageSize", pageSize),
										new SqlParameter("@TotalRecord", totalRecord)
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@TotalRecord"].Direction = ParameterDirection.Output;
                var OutBillDetailList = DbHelper.ExecuteSqlList<OutBillDetail>(sqlCommand);
				if (sqlCommand.Parameters["@TotalRecord"].Value != null)
                {
                    totalRecord = (int) sqlCommand.Parameters["@TotalRecord"].Value;
                }

                return OutBillDetailList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutBillDetail>();
            }
        }

        public OutBillDetail GetOutBillDetailById(int billDetailID)
        {
            try
            {
                const string spName = "spo_OutBillDetail_GetById";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillDetailID", billDetailID)
                                    };

                var OutBillDetail = DbHelper.ExecuteSPList<OutBillDetail>(spName, sqlParams).SingleOrDefault();
                return OutBillDetail;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return null;
            }
        }

        public int InsertOutBillDetail(OutBillDetail OutBillDetail)
        {
            try
            {
                const string spName = "spo_OutBillDetail_Insert";
                var sqlParams = new[]
                                    {

										new SqlParameter("@BillID", OutBillDetail.BillID),
										new SqlParameter("@ProID", OutBillDetail.ProID),
										new SqlParameter("@BillDetailCnt", OutBillDetail.BillDetailCnt),
										new SqlParameter("@BillDetailID", OutBillDetail.BillDetailID),

                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
				sqlCommand.Parameters["@BillDetailID"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@BillDetailID"].Value != null)
                {
                    return (int) sqlCommand.Parameters["@BillDetailID"].Value;
                }

				return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int UpdateOutBillDetail(OutBillDetail OutBillDetail)
        {
            try
            {
                const string spName = "spo_OutBillDetail_Update";
                var sqlParams = new[]
                                    {
										new SqlParameter("@BillDetailID", OutBillDetail.BillDetailID)
										, new SqlParameter("@BillID", OutBillDetail.BillID)
										, new SqlParameter("@ProID", OutBillDetail.ProID)
										, new SqlParameter("@BillDetailCnt", OutBillDetail.BillDetailCnt)
                                    };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutBillDetail(int billDetailID)
        {
            try
            {
                const string spName = "spo_OutBillDetail_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@BillDetailID", billDetailID)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }
		#endregion
    }
}
